<template>
  <div class="user-center-container">
    <div class="user-center-header">
      <h1>个人中心</h1>
    </div>
    
    <div class="user-center-content">
      <!-- 侧边栏导航 -->
      <aside class="user-sidebar">
        <div class="user-profile-summary">
          <div class="avatar">
            <img v-if="userStore.userInfo.avatar" :src="userStore.userInfo.avatar" alt="用户头像" />
            <div v-else class="default-avatar">
              {{ getAvatarText }}
            </div>
          </div>
          <div class="user-info">
            <h3>{{ userStore.userInfo.username || '未登录' }}</h3>
            <p class="user-email">{{ userStore.userInfo.email }}</p>
          </div>
        </div>
        
        <nav class="user-nav">
          <ul>
            <li>
                <router-link
                  :to="'/user/profile'"
                  class="nav-item"
                  :class="{ 'active': currentRoute === '/user/profile' }"
                >
                  <span class="nav-icon">👤</span>
                  <span class="nav-text">个人信息</span>
                </router-link>
              </li>
              <li>
                <router-link
                  :to="'/user/membership'"
                  class="nav-item"
                  :class="{ 'active': currentRoute === '/user/membership' }"
                >
                  <span class="nav-icon">🎫</span>
                  <span class="nav-text">会员中心</span>
                </router-link>
              </li>
            <li>
              <router-link
                :to="'/user/orders'"
                class="nav-item"
                :class="{ 'active': currentRoute === '/user/orders' }"
              >
                <span class="nav-icon">📦</span>
                <span class="nav-text">我的订单</span>
                <span v-if="userStore.pendingOrders.length > 0" class="order-badge">
                  {{ userStore.pendingOrders.length }}
                </span>
              </router-link>
            </li>
            <li>
              <router-link
                :to="'/user/addresses'"
                class="nav-item"
                :class="{ 'active': currentRoute === '/user/addresses' }"
              >
                <span class="nav-icon">🏠</span>
                <span class="nav-text">收货地址</span>
              </router-link>
            </li>
            <li>
              <a href="#" class="nav-item">
                <span class="nav-icon">🎁</span>
                <span class="nav-text">我的收藏</span>
              </a>
            </li>
            <li>
              <a href="#" class="nav-item">
                <span class="nav-icon">💰</span>
                <span class="nav-text">账户余额</span>
              </a>
            </li>
            <li>
              <a href="#" class="nav-item">
                <span class="nav-icon">⚙️</span>
                <span class="nav-text">账户设置</span>
              </a>
            </li>
          </ul>
        </nav>
        
        <button class="logout-button" @click="handleLogout">
          退出登录
        </button>
      </aside>
      
      <!-- 主内容区域 -->
      <main class="user-main-content">
        <RouterView />
      </main>
    </div>
  </div>
</template>

<script setup>
import { computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useUserStore } from '../stores/user'

const route = useRoute()
const router = useRouter()
const userStore = useUserStore()

// 获取当前路由
const currentRoute = computed(() => route.path)

// 获取头像显示文字
const getAvatarText = computed(() => {
  if (userStore.userInfo.username) {
    return userStore.userInfo.username.charAt(0).toUpperCase()
  }
  return '?'
})

// 处理退出登录
const handleLogout = () => {
  if (confirm('确定要退出登录吗？')) {
    userStore.logout()
    router.push('/login')
  }
}

// 组件挂载时检查登录状态
onMounted(() => {
  // 从本地存储加载用户状态
  userStore.loadFromLocalStorage()
  
  // 如果未登录，跳转到登录页面
  if (!userStore.isLoggedIn) {
    router.push('/login')
  }
})
</script>

<style scoped>
.user-center-container {
  min-height: 100vh;
  background-color: #f5f5f5;
}

.user-center-header {
  background-color: #ffffff;
  padding: 20px 0;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
}

.user-center-header h1 {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  font-size: 24px;
  color: #333;
}

.user-center-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  gap: 20px;
}

/* 侧边栏样式 */
.user-sidebar {
  width: 260px;
  background-color: #ffffff;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  padding: 24px;
  flex-shrink: 0;
}

.user-profile-summary {
  display: flex;
  align-items: center;
  gap: 16px;
  padding-bottom: 24px;
  border-bottom: 1px solid #eee;
  margin-bottom: 24px;
}

.avatar {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  overflow: hidden;
  background-color: #f0f0f0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.default-avatar {
  font-size: 24px;
  font-weight: 600;
  color: #4285f4;
}

.user-info h3 {
  margin: 0 0 4px 0;
  font-size: 18px;
  color: #333;
}

.user-email {
  margin: 0;
  font-size: 14px;
  color: #666;
}

/* 导航样式 */
.user-nav ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.user-nav li {
  margin-bottom: 8px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-radius: 8px;
  color: #666;
  text-decoration: none;
  transition: all 0.3s ease;
  font-size: 15px;
  position: relative;
}

.nav-item:hover {
  background-color: #f8f9fa;
  color: #4285f4;
}

.nav-item.active {
  background-color: #e8f0fe;
  color: #4285f4;
  font-weight: 500;
}

.nav-icon {
  font-size: 18px;
}

.order-badge {
  position: absolute;
  right: 16px;
  background-color: #d32f2f;
  color: white;
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 12px;
  min-width: 20px;
  text-align: center;
}

/* 退出登录按钮 */
.logout-button {
  margin-top: 32px;
  width: 100%;
  padding: 12px;
  background-color: #f5f5f5;
  color: #666;
  border: none;
  border-radius: 8px;
  font-size: 15px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.logout-button:hover {
  background-color: #e0e0e0;
  color: #333;
}

/* 主内容区域 */
.user-main-content {
  flex: 1;
  background-color: #ffffff;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  min-height: 600px;
}

@media (max-width: 768px) {
  .user-center-content {
    flex-direction: column;
  }
  
  .user-sidebar {
    width: 100%;
  }
  
  .user-profile-summary {
    justify-content: center;
  }
  
  .user-nav ul {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
  }
  
  .user-nav li {
    margin-bottom: 0;
    flex: 1;
    min-width: calc(50% - 4px);
  }
  
  .nav-item {
    justify-content: center;
    padding: 10px;
  }
  
  .logout-button {
    margin-top: 20px;
  }
}
</style>